$(function () {
  $('#btnQuery').click(function () {
    let keyword = $('[name=keyword]').val()
    let obj = {
      keyword,
    }
    $.post('/Product/query', obj, function (res) {
      $('[name=tbRow]').html('')
      res.forEach(item=>{
        let html=`
        <tr class="tbRow" name="tbRow">
        <td>${item.id}</td>
      <td>${item.productName}</td>
        <td>${item.supplier}</td>
           <td>${item.price}</td>
         <td>${item.stock}</td>
        <td>${item.remarks}</td>


            <td>
      <input class="btnEdit" onclick="btnEdit(${item.id})" type="button" value="编辑">
           <input class="btnDel" onclick="btnDel(${item.id})" type="button" value="删除">
            </td>
        </tr>
        `
        $('#tbData').append(html)
      })
    })
  })
  



  $('#btnAdd').click(function () {
    window.location.href = `/Product/createOrEdit`
  })

  $('#btnSave').click(function () {
    let id = $('[name=id]').val()
    let ProductName = $('[name=ProductName]').val()
    let Supplier = $('[name=Supplier]').val()
    let Price = $('[name=Price]').val()
    let Stock = $('[name=Stock]').val()
    let remarks = $('[name=remarks]').val()

    
    let obj = {
      id,
      ProductName,
      Supplier,
      Price,
      Stock,
      remarks,
    }
    $.post('/Product/save', obj, function () {
      alert('保存成功！')
      window.location.href = `/Product/Index`
    })
  })
  $('#btnCancel').click(function () {
    window.location.href = `/Product/index`
  })
})
function btnEdit(id) {
  window.location.href = `/Product/createOrEdit/${id}`
}

